package com.hp.controller;

import com.github.pagehelper.PageInfo;
import com.hp.entity.Announcement;
import com.hp.service.AnnouncementService;
import com.hp.util.ResultJson;
import com.hp.util.Utils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/announ")
public class AnnouncementController {
    @Resource
    AnnouncementService announcementService;


    @RequestMapping("queryAll")
    public ModelAndView queryAll(Announcement announcement, Integer page, Integer limit, HttpServletResponse response) {
        ModelAndView model = new ModelAndView();
        System.out.println(announcement+"----------hisAnnouncement-------------");
        List<Announcement> list = announcementService.queryAll(announcement,page,limit);
        PageInfo<Announcement> userPageInfo = new PageInfo<Announcement>(list);
        System.out.println(list+"-----------list---------------");
        Utils.returnJson(ResultJson.success(userPageInfo), response);
        return model;
    }
    @RequestMapping("/insert")
    public String insert(Announcement announcement, HttpServletResponse resp) throws Exception {
        System.out.println(announcement);
        int i = announcementService.insert(announcement);
        System.out.println(i + "----------222--------");
        Utils.returnJson(ResultJson.success(i), resp);
        return null;
    }
    @RequestMapping("/findId")
    public ModelAndView findId(Integer id, HttpServletRequest req, HttpServletResponse resp) {
        Announcement hisa = announcementService.findId(id);
        System.out.println(hisa + "<<<<<<<<<id>>>>>>>>>");
        ModelAndView modelAndView = new ModelAndView();
        Utils.returnJson(ResultJson.success(hisa), resp);
        return null;
    }
    @RequestMapping("/update")
    public String update(Announcement announcement, HttpServletResponse resp) throws Exception {
        int i = announcementService.update(announcement);
        System.out.println(i + "----------111--------");
        Utils.returnJson(ResultJson.success(i), resp);
        return null;
    }
    @RequestMapping("/del")
    public ModelAndView del(Integer id, HttpServletResponse resp) {
        ModelAndView model = new ModelAndView();
        System.out.println(id);
        int i = announcementService.del(id);
        Utils.returnJson(ResultJson.success(i), resp);
        return model;
    }

}
